package cn.shop.crontab.task;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.crontab.mapper.FrontUserMapper;
import cn.shop.crontab.service.WithdrawService;
import cn.shop.transcation.backSetting.entity.BackSetting;
import cn.shop.transcation.backSetting.mapper.BackSettingMapper;
import cn.shop.transcation.bankcard.entity.BankCard;
import cn.shop.transcation.bankcard.mapper.BankCardMapper;
import cn.shop.transcation.frontUserModule.FrontUser;
import cn.shop.transcation.withdraw.entity.Withdraw;
import cn.shop.transcation.withdraw.mapper.WithdrawMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import static cn.shop.utils.constant.SystemSettingConstant.*;

@Slf4j
@Service
public class AutoWithdrawTask {

    @Resource
    private FrontUserMapper userMapper;
    @Resource
    private BackSettingMapper settingMapper;
    @Resource
    private BankCardMapper cardMapper;
    @Resource
    private WithdrawService service;
    @Resource
    private WithdrawMapper mapper;

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void autoWithdraw() {
        log.info("开始自动提现");
        List<BackSetting> settings = settingMapper.selectList(Wrappers.<BackSetting>lambdaQuery()
                .in(BackSetting::getKey, Arrays.asList(AUTO_WITHDRAW_MIN, AUTO_WITHDRAW_MAX, WITHDRAW_DAY_COUNT,P_BANK_WITHDRAW_SHARE, P_BANK_WITHDRAW_SUB)));
        BigDecimal min = BigDecimal.ZERO ,max = BigDecimal.ZERO;
        String v = "0", v2 = "0";
        for (BackSetting setting : settings) {
            if (StrUtil.equals(setting.getKey(), P_BANK_WITHDRAW_SHARE)) {
                v = setting.getValue();
            }
            if (StrUtil.equals(setting.getKey(), P_BANK_WITHDRAW_SUB)) {
                v2 = setting.getValue();
            }
        }
        long times = 0L;
        for (BackSetting setting : settings) {
            switch (setting.getKey()) {
                case AUTO_WITHDRAW_MIN:
                    min = new BigDecimal(setting.getValue());
                    break;
                case AUTO_WITHDRAW_MAX:
                    max = new BigDecimal(setting.getValue());
                    break;
                case WITHDRAW_DAY_COUNT:
                    times = NumberUtil.parseInt(setting.getValue());
                    break;
            }
        }
        List<FrontUser> users = userMapper.selectList(Wrappers.<FrontUser>lambdaQuery()
                .eq(FrontUser::getAutoWithdraw, true)
                .eq(FrontUser::getRisk, false)
                .eq(FrontUser::getAdvanced, false)
                .gt(FrontUser::getBalance, min));
        LocalDate now = LocalDate.now();
        for (FrontUser user : users) {
            BankCard card = cardMapper.selectOne(Wrappers.<BankCard>lambdaQuery()
                            .eq(BankCard::getOwnerId, user.getId())
                            .eq(BankCard::getCardDefault, true));
            if (card == null) {
                continue;
            }
            if (user.getAdvanced()) {
                continue;
            }
            if (user.getRisk()) {
                continue;
            }
            if (times > 0) {
                long count = mapper.selectCount(Wrappers.<Withdraw>lambdaQuery()
                        .eq(Withdraw::getUserId, user.getId())
                        .gt(Withdraw::getCreateTime, now.atTime(0, 0, 0))
                        .lt(Withdraw::getCreateTime, now.atTime(23,59,59)));
                if (count >= times) {
                    continue;
                }
            }
            BigDecimal amount = BigDecimal.ZERO;
            if (NumberUtil.isGreater(user.getBalance(), max)) {
                amount = max;
            } else {
                amount = user.getBalance();
            }
            log.info("提现金额： {}", amount);
            if (NumberUtil.isGreater(amount, BigDecimal.ZERO)) {
                try {
                    service.withdraw(user, card, amount, v, v2);
                } catch (Exception e) {
                    log.warn("", e);
                }
            }
        }
        log.info("结束自动提现");
    }

}
